import 'dart:ui';

class ColorUtil {
  /// 根据 [ratio] 在数组的范围计算色彩
  static Color computeColor(
      double ratio, List<double> ratios, List<Color> colors) {
    assert(ratios.length == colors.length, 'ratios和colors的长度不等');
    for (var i = 0; i < ratios.length; i++) {
      var r = ratios[i];
      if (ratio == r) {
        return colors[i];
      } else if (ratio < r) {
        var index = i - 1;
        if (index < 0) return colors[0];
        var lastRatio = ratios[index];
        var bias = (ratio - lastRatio) / (r - lastRatio);
        return Color.lerp(colors[index], colors[i], bias)!;
      }
    }
    return colors.last;
  }
}
